﻿using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.PublicShow
{
    public partial class CreateCode : System.Web.UI.Page
    {
        private BaseBLL baseBLL = new BaseBLL();
        private QS_PROJECTBLL projectBLL = new QS_PROJECTBLL();
        private QS_PROJECT_CODEBLL procodeBLL = new QS_PROJECT_CODEBLL();

        /// <summary>
        /// 加载界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTreeView();
            }
        }

        /// <summary>
        /// 绑定TreeView
        /// </summary>
        private void BindTreeView()
        {
            if (Request["PROID"] != null && !string.IsNullOrEmpty(Request["PROID"].ToString()))
            {
                MyTree.Nodes.Clear();

                #region 获得项目名称,添加至节点

                DataTable dt_project = projectBLL.SelectBy(" AND N_PROID = " + Request["PROID"].ToString());

                if (dt_project == null || dt_project.Rows.Count == 0)
                {
                    return;
                }

                string projectName = dt_project.Rows[0]["C_PRONAME"].ToString();

                TreeNode proNode = new TreeNode(projectName, "0");
                MyTree.Nodes.Add(proNode);

                #endregion 获得项目名称,添加至节点

                #region 获得该项目下的关键字,添加至节点

                DataTable dt_procode = procodeBLL.Select(" AND N_PROID = " + Request["PROID"].ToString());

                if (dt_procode == null || dt_procode.Rows.Count == 0)
                {
                    return;
                }

                foreach (DataRow dr in dt_procode.Rows)
                {
                    TreeNode code = new TreeNode(dr["C_CODE"].ToString(), dr["N_CODEID"].ToString());
                    proNode.ChildNodes.Add(code);
                }

                #endregion 获得该项目下的关键字,添加至节点
            }
        }

        /// <summary>
        /// 添加关键字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(FORM_CODE.Text.Trim()))
            {
                MessageBox.Show(Page, "请输入关键字");
                return;
            }

            QS_PROJECT_CODE procode = new QS_PROJECT_CODE();
            procode.N_PROID = Convert.ToInt32(Request["PROID"].ToString());
            procode.C_CODE = FORM_CODE.Text.Trim();

            try
            {
                int number = procodeBLL.Insert(procode);
                if (number > 0)
                {
                    BindTreeView();
                    MessageBox.Show(Page, "添加关键字成功");
                }
                else
                {
                    MessageBox.Show(Page, "添加关键字失败,关键字可能重复添加");
                }
            }
            catch
            {
                MessageBox.Show(Page, "添加关键字失败");
            }
        }

        /// <summary>
        /// 删除关键字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            TreeNodeCollection tnc = MyTree.CheckedNodes;

            string codeId = "";

            foreach (TreeNode item in tnc)
            {
                if (item.Checked && item.Value != "0")
                {
                    codeId += item.Value + ",";
                }
            }

            if (codeId == "")
            {
                MessageBox.Show(Page, "请选择一项关键字");
                return;
            }

            try
            {
                codeId = codeId.Substring(0, codeId.Length - 1);
                procodeBLL.Delete(codeId);
                BindTreeView();
                MessageBox.Show(Page, "删除关键字成功");
            }
            catch
            {
                MessageBox.Show(Page, "删除关键字失败");
            }
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOk_Click(object sender, EventArgs e)
        {
            TreeNodeCollection tnc = MyTree.Nodes[0].ChildNodes;
            try
            {
                int count = 0;
                string Ids = "";
                string Name = "";
                foreach (TreeNode item in tnc)
                {
                    if (item.Value != "0")
                    {
                        count += 1;
                        Name += item.Text + ",";
                        Ids += item.Value + ",";
                    }
                }

                if (count > 0)
                {
                    Ids = Ids.Substring(0, Ids.Length - 1);
                    Name = Name.Substring(0, Name.Length - 1);
                }

                MessageBox.ResponseScript(this, "FORM_SUBMIT(\"" + Ids + "%" + Name + "\");");
            }
            catch (Exception exp)
            {
                MessageBox.Show(Page, exp.Message.ToString());
                return;
            }
        }
    }
}